#include "datahandler.h"
const char header = 0xff;
const char PnkID = 0xfe;
const char RngID = 0xfd;
const char MddlID = 0xfc;
const char PntrID = 0xfb;
const char ThumbID = 0xfa;

dataHandler::dataHandler(QObject *parent) :
    QObject(parent)
{
    //do I need something here?
}

void dataHandler::serialData(QByteArray sbytes)
{
    sArray.append(sbytes);
    //this is going ot need to change....
//    if(sArray.size()>=14)  //are there enough bytes to be a full finger
//    {
    int removal;
    do
    {
        removal = sArray.indexOf(header);
        if(removal != -1)
        {
            sArray.remove(sArray.length()-1, sArray.length()-removal);  //remove everything up through the first header.
            if(sArray.length()>6)
            {
                //Array.remove(sArray.length() - 1, 1);
                fingerParser(sArray[sArray.length()-1]);    //this should be a Finger ID
            }
        }
        else sArray.clear();
    }
    while(sArray.size()>7); //don't try parsing unless there is at least a finger worth of bytes
//    }
}

void dataHandler::fingerParser(char FID)
{
    finger tmp;
    //sArray.remove(sArray.length() - 1, 1);    //remove the finger ID
    //parse the finger and check the validity byte
    //sArray.remove(sArray.length() - 1, 1);
    tmp.tip = sArray[sArray.length()-2];
    //sArray.remove(sArray.length() - 1, 1);
    tmp.mid = sArray[sArray.length()-3];//1];
    //sArray.remove(sArray.length() - 1, 1);
    tmp.kncl = sArray[sArray.length()-4];
    //sArray.remove(sArray.length() - 1, 1);
    tmp.swp = sArray[sArray.length()-5];
    //sArray.remove(sArray.length() - 1, 1);
    tmp.chckr = FID ^ tmp.tip ^ tmp.mid ^ tmp.kncl ^ tmp.swp;
    QString message;
    switch(FID)
    {
    case PnkID:
        message = "Pinky:  ";
        break;
    case RngID:
        message = "Ring:  ";
        break;
    case MddlID:
        message = "Middle:  ";
        break;
    case PntrID:
        message = "Pointer:  ";
        break;
    case ThumbID:
        message = "Thumb:  ";
        break;
    default:
        message = "Unknown ID:  ";
        break;
    }

    if(tmp.chckr == sArray[sArray.length()-6])
    {
        //validity byte match, decide what to do with data
        //right now that is immediately emit. It could be some processing in the future
        emit tipNew(tmp.tip);
        emit middleNew(tmp.mid);
        emit knuckleNew(tmp.kncl);
        emit sweepNew(tmp.swp);
        message.append(tmp.tip);
        message.append("  ");
        message.append(tmp.mid);
        message.append("  ");
        message.append(tmp.kncl);
        message.append("  ");
        message.append(tmp.swp);
//                    message.append("  ");
//                    message.append(tmp.chckr);    //its valid so we don't need to print it
    }
    else
    {
        //validity byte mismatch, print data with error message but don't update anything else
        message.prepend("VALIDITY BYTE MISMATCH ");
        message.append(tmp.tip);
        message.append("  ");
        message.append(tmp.mid);
        message.append("  ");
        message.append(tmp.kncl);
        message.append("  ");
        message.append(tmp.swp);
        message.append("  ");
        message.append(tmp.chckr);
        message.append("  ");
        tmp.chckr = sArray[sArray.length()-1];
        message.append(tmp.chckr);
        //message.append("\nEND INVALID DATA")
    }
    sArray.remove(sArray.length() - 1, 6);
    emit logMessage(message);
}
